﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;

namespace Iacym.Repository
{
    public class LineaTelefonicaRepository : MasterRepository, ILineaTelefonicaRepository
    {
        public List<LINEA_TELEFONICA> GetAllFromLineaTelefonica()
        {
            var query = from p in context.LINEA_TELEFONICA
                        select p;

            return query.ToList();
        }

        public List<LINEA_TELEFONICA> GetAllFromLineaTelefonicaByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.LINEA_TELEFONICA
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;

                return query.ToList();
            }
            else
            {
                var query = from p in context.LINEA_TELEFONICA
                            select p;

                return query.ToList();
            }
        }

        public LINEA_TELEFONICA GetFromLineaTelefonica(int id)
        {
            var query = from p in context.LINEA_TELEFONICA
                        where p.Id.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddLinea(LINEA_TELEFONICA linea)
        {
            context.LINEA_TELEFONICA.Add(linea);
            context.SaveChanges();
        }

        public void UpdateLinea(LINEA_TELEFONICA linea)
        {
            context.Entry(linea).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteLinea(int id)
        {
            var existe = context.LINEA_TELEFONICA.Where(p => p.Id.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                context.LINEA_TELEFONICA.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from p in context.LINEA_TELEFONICA
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        && p.Id != id
                        select p;

            return query.Any();
        }
    }
}
